package com.farm.controller.shiro;

import com.farm.result.Result;
import com.farm.shiro.service.impl.AccountService;
import com.farm.shiro.util.ShiroUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * AccountController
 *
 * @author ctidy
 * @since 2021/3/25
 */
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class AccountController {

    public final AccountService accountService;

    @PostMapping("/logon")
    public Result logon(@RequestParam String username, @RequestParam("password") String plainPassword) {
        boolean flag = accountService.register(username, plainPassword);
        return Result.judge(flag, null, "用户名重复");
    }

    @PostMapping("/login")
    public Result login(@RequestParam String username, @RequestParam("password") String plainPassword) {
        ShiroUtils.login(username, plainPassword);
        return Result.succeed();
    }

    @GetMapping("/logout")
    public Result logout() {
        ShiroUtils.logout();
        return Result.succeed();
    }

}
